// Copyright 2011 Jefferson Willian
// willmont7@gmail.com

#include <cstdio>

int main() {
  int n;
  int temp;
  int game = 1;
  int letters[5][2];
  int digits[6][10];
  char l;

  while (scanf("%d", &n) && n != 0) {
    for (int i = 0;i < 6;i++)
      for (int j = 0;j < 10;j++)
        digits[i][j] = 0;

    for (int i = 0;i < n;i++) {
      for (int j = 0;j < 5;j++) {
        scanf("%d", &temp);
        letters[j][0] = temp;
        scanf("%d", &temp);
        letters[j][1] = temp;
      }

      for (int j = 0;j < 6;j++) {
        getchar();
        scanf("%c", &l);
        l -= 'A';
        /* Incrementa os provaveis digitos da senha */
        digits[j][letters[l][0]]++;
        digits[j][letters[l][1]]++;
      }
    }

    printf("Teste %d\n", game++);

    /* A senha sera aqueles numeros cujos indices foram incrementados n vezes */
    for (int i = 0;i < 6;i++)
      for (int j = 0;j < 10;j++)
        if (digits[i][j] == n) {
          printf("%d ", j);
          break;
        }

    printf("\n\n");
  }

  return 0;
}
